package cn.edu.zut.cs.Controller;

import cn.edu.zut.cs.Model.Patient;
import cn.edu.zut.cs.Service.WPService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PatientController {

    WPService service;

    //主页
    @RequestMapping(value = "/index.html")
    public String index() {
        return "index";
    }
    //病人列表
    @RequestMapping(value = "/patientlist.html")
    public String patientlist(HttpServletRequest request) {
        request.getSession().setAttribute("patients",  service.queryAllPatients());
        return "patientlist";
    }
    //病房列表
    @RequestMapping(value = "/wardList.html")
    public String wardList(HttpServletRequest request) {
        request.getSession().setAttribute("wards",  service.queryAllWard());
        return "wardList";
    }
    //具体病房情况
    @RequestMapping(value = "/wardMore.html")
    public String wardMore(HttpServletRequest request,String wardid) {
        //String wardid= (String) request.getAttribute("wardid");
        request.getSession().setAttribute("patients",  service.queryByWard(wardid));
        request.getSession().setAttribute("twardid",  wardid);
        return "ward";
    }
    //插入病人
    @RequestMapping(value = "/newPatient.html")
    public String newPatient(HttpServletRequest request) {
        return "newPatient";
    }
    //插入病人时选择病房
    @RequestMapping(value = "/selectWard.html")
    public String selectWard(HttpServletRequest request, Patient patient) {
        request.getSession().setAttribute("tpatient",  patient);
        request.getSession().setAttribute("wards",  service.queryByType(patient.getStatus()));
        return "selectWard";
    }
    //查询病人
    @RequestMapping(value = "/selectPatient.html")
    public String selectPatient(HttpServletRequest request) {
        //request.getSession().setAttribute("patients", null);
        return "selectPatient";
    }
    //根据编号查询病人，可编辑
    @RequestMapping(value = "/queryByPno.html")
    public String queryByPno(HttpServletRequest request, Patient patient) {
        Patient p1=service.queryByPno(patient.getPno());
        request.getSession().setAttribute("tpatient",p1);
        return "modify";
    }
    //根据姓名非具体查询，不可编辑
    @RequestMapping(value = "/queryByName.html")
    public String queryByName(HttpServletRequest request, Patient patient) {
        request.getSession().setAttribute("patients",service.queryByName(patient.getName()));
        return "selectPatient";
    }
    //根据编号进行删除操作
    @RequestMapping(value = "/deleteByPno.html")
    public String deleteByPno(HttpServletRequest request,Patient patient) {
        String patientId=patient.getPno();
        service.deleteWP(patientId);
        request.getSession().setAttribute("patients",  service.queryAllPatients());
        return "patientlist";
    }
    //依据病人进行修改操作，prepatient为修改前的病人，patient为修稿后的病人
    @RequestMapping(value = "/modifyPatient.html")
    public String modify(HttpServletRequest request,Patient patient) {
        Patient prepatient=(Patient) request.getSession().getAttribute("tpatient");
        service.modifyWP(prepatient,patient);
        request.getSession().setAttribute("patients",  service.queryAllPatients());
        return "patientlist";
    }
    //修改前进行的填充可编辑页面操作
    @RequestMapping(value = "/modifyQuery.html")
    public String modifyQuery(HttpServletRequest request,String pno) {
        request.getSession().setAttribute("tpatient",  service.queryByPno(pno));
        return "modify";
    }
    //新建病人自动匹配病房并分配床位
    @RequestMapping(value = "/insert.html")
    public String insert(HttpServletRequest request,Patient patient) {
        service.insertWP(patient);
        request.getSession().setAttribute("patients",service.queryAllPatients());
        return "patientlist";
    }
    //新建病人选择病房的页面
    @RequestMapping(value = "/sureWard.html")
    public String sureWard(HttpServletRequest request,String wId) {
        Patient p1=(Patient) request.getSession().getAttribute("tpatient");
        p1.setWardId(wId);
        service.insertWP(p1,wId);
        request.getSession().setAttribute("patients",service.queryAllPatients());
        return "patientlist";
    }
    //删库跑路
    @RequestMapping(value = "/deleteAll.html")
    public String deleteAll(HttpServletRequest request) {
        service.deleteAllPatients();
        request.getSession().setAttribute("patients",  service.queryAllPatients());
        return "patientlist";
    }
    @Autowired
    public void setService(WPService service) {
        this.service = service;
    }
}

